/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wikikii.revealbanner.slice;

import com.bumptech.glide.Glide;
import com.wikikii.bannerlib.banner.IndicatorLocation;
import com.wikikii.bannerlib.banner.LoopLayout;
import com.wikikii.bannerlib.banner.LoopStyle;
import com.wikikii.bannerlib.banner.OnDefaultImageViewLoader;
import com.wikikii.bannerlib.banner.bean.BannerInfo;
import com.wikikii.bannerlib.banner.listener.OnBannerItemClickListener;
import com.wikikii.bannerlib.banner.view.BannerBgContainer;
import com.wikikii.revealbanner.RecyclerViewAbility;
import com.wikikii.revealbanner.ResourceTable;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Image;

import java.util.ArrayList;
import java.util.List;

/**
 * BannerAbilitySlice
 *
 * @since 2021-06-09
 */
public class BannerAbilitySlice extends AbilitySlice implements OnBannerItemClickListener {
    BannerBgContainer bannerBgContainer;
    LoopLayout loopLayout;
    Button btnList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_banner);

        bannerBgContainer = (BannerBgContainer) findComponentById(ResourceTable.Id_banner_bg_container);
        loopLayout = (LoopLayout) findComponentById(ResourceTable.Id_loop_layout);
        btnList = (Button) findComponentById(ResourceTable.Id_btn_list);

        btnList.setClickedListener(component -> launchAbility(new RecyclerViewAbility()));
        loopLayout.setLoop_ms(3000); // 轮播的速度(毫秒)
        loopLayout.setLoop_duration(400); // 滑动的速率(毫秒)
        loopLayout.setScaleAnimation(true); // 设置是否需要动画
        loopLayout.setIndicatorLocation(IndicatorLocation.Center); // 指示器位置-中Center
        loopLayout.initializeData(this);

        // 准备数据
        ArrayList<BannerInfo> bannerInfos = new ArrayList<>();
        List<Object> bgList = new ArrayList<>();
        bannerInfos.add(new BannerInfo(ResourceTable.Media_banner_1, "first"));
        bannerInfos.add(new BannerInfo(ResourceTable.Media_banner_2, "second"));
        bgList.add(ResourceTable.Media_banner_bg1);
        bgList.add(ResourceTable.Media_banner_bg2);

        // 设置监听
        loopLayout.setOnLoadImageViewListener(new OnDefaultImageViewLoader() {
            @Override
            public void onLoadImageView(Image view, Object object) {
                Glide.with(view.getContext())
                        .load(object)
                        .into(view);
            }
        });
        loopLayout.setOnBannerItemClickListener(this);
        if (bannerInfos.size() == 0) {
            return;
        }
        loopLayout.setLoopData(bannerInfos);
        bannerBgContainer.setBannerBackBg(this, bgList);
        loopLayout.setBannerBgContainer(bannerBgContainer);
        loopLayout.startLoop();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    private void launchAbility(Ability ability) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(getBundleName())
                .withDeviceId("")
                .withAbilityName(ability.getClass().getName())
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    @Override
    public void onBannerClick(int index, ArrayList<BannerInfo> banner) {
    }
}
